'''
Created on 2012-9-19

@author: zshe
'''
import types
from BaseDao import BaseDao

class UserDao(BaseDao):
    '''
    User dao
    '''
    __sqlTemplate = '''INSERT INTO WeiboUser (idUser, screen_name, name, province, city,
            location, description, url, profile_image, domain, gender, avatar_large,
            verified, verified_reason, INSERT_TIMESTAMP, LAST_UPDATE_TIMESTAMP)
        VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
            current_timestamp, current_timestamp);'''

    def __init__(self):
        '''
        Pass
        '''
        pass
    
    def insertUsers(self, users):
        assert(type(users) is types.ListType)
        
        rows = []
        for user in users:
            assert(type(user) is types.DictionaryType)
            rows.append(self.__mapRow(user))
        
        self.executemany(self.__sqlTemplate, rows)
    
    def __mapRow(self, user):
        if type(user['gender']) != types.IntType:
            if user['gender'].find('m') != -1:
                user['gender'] = 1
            elif user['gender'].find('f') != -1:
                user['gender'] = 2
            else:
                user['gender'] = 3
        
        return (user['id'], user['screen_name'], user['name'], user['province'],
                user['city'], user['location'], user['description'], user['url'],
                user['profile_image'], user['domain'], user['gender'], user['avatar_large'],
                user['verified'], user['verified_reason'])